package com.xqyq.controller.manage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xqyq.model.Match;
import com.xqyq.model.Sign;
import com.xqyq.service.MatchService;
import com.xqyq.service.SignService;
import com.xqyq.util.SignTemp;
@RequestMapping("/manage")
@Controller
public class SysSignContorller {

	@Autowired
	private MatchService matchService;

	@Autowired
	private SignService signService;

	@RequestMapping("/sign_list.html")
	public String sign_list(@RequestParam("match_id") int match_id,
			HttpServletRequest request) {
		Match match = matchService.getMatchWithSign(match_id);
		request.setAttribute("match", match);
		return "manage/sign_list";
	}

	@RequestMapping("/api_getSignInMatch")
	@ResponseBody
	public String api_getSignList(@RequestParam("match_id") int match_id)
			throws JsonGenerationException, JsonMappingException, IOException {
		List<Sign> signList = signService.getSignListNoPrize(match_id);
		List<SignTemp> list = new ArrayList<SignTemp>();
		SignTemp signTemp = null;
		for (int i = 0; i < signList.size(); i++) {
			signTemp = new SignTemp();
			signTemp.setSign_id(signList.get(i).getSign_id());
			signTemp.setUser_name(signList.get(i).getUser().getUser_name());
			list.add(signTemp);
		}
		return new ObjectMapper().writeValueAsString(list);
	}

	@RequestMapping("/signup_check.html")
	public String signcheck(HttpServletRequest request) {
		List<Sign> signList = signService.getSignListNoCheck();
		request.setAttribute("signList", signList);
		return "manage/signup_check";
	}

	@RequestMapping("/api_deleteSign")
	public String deleteSign(@RequestParam("sign_id") int sign_id) {
		signService.deleteSign(sign_id);
		return "redirect:signup_check.html";
	}

	@RequestMapping("/api_passSign")
	public String passSign(@RequestParam("sign_id") int sign_id) {
		signService.passSign(sign_id);
		return "redirect:signup_check.html";
	}
}
